package com.icow.wms.basic.controller;

import com.icow.wcs.dao.OperatingPointDao;
import com.icow.wcs.dao.ShelfDao;
import com.icow.wcs.domain.*;
import com.icow.wcs.obj.LoadStatusEnum;
import com.icow.wcs.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

import static org.springframework.util.StringUtils.isEmpty;

@Controller
@RequestMapping(value = {"/wms.basic/shelf", "/api/shelf"})
public class OperationPointController {
    @Autowired
    private ShelfDao shelfDao;
    @Autowired
    private OperatingPointDao operatingPointDao;

    @RequestMapping("/unloadPoint/index")
    String unloadPointIndex() {
        return "unload_point_index";
    }

    @RequestMapping("/unloadPoint/save")
    @ResponseBody
    Result save(OperatingPointDomain domain) {
        String locationName = domain.getLocationName();
        int level = domain.getLevel();
        if (isEmpty(locationName)) {
            return Result.error("至少选择一个点位");
        }
        OperatingPointDomain find = operatingPointDao.findByLocationNameAndType(locationName, OperatingPointType.UNLOAD.getValue());
        if (find != null) {
            return Result.error("点位已存在");//入库点和出库点可以是同一个
        }
        ShelfDomain find2 = shelfDao.findByLocationName(locationName);
        if (find2 != null) {
            return Result.error("点位已存在");
        }

        domain.setType(OperatingPointType.UNLOAD.getValue());
        if(domain.getCreatedDate()==null) {
            domain.setCreatedDate(new Date());
        }

        OperatingPointDomain saved = operatingPointDao.save(domain);
        return Result.success(saved.getId());
    }

    @RequestMapping("/unloadPoint/list")
    @ResponseBody
    PageResult<OperatingPointDomain> listUnloadPoint(PageParam param) {

        PageRequest pr = PageRequest.of(param.getStart() / param.getLength(), param.getLength());

        Page<OperatingPointDomain> page = operatingPointDao.findByType(OperatingPointType.UNLOAD.getValue(), pr);

        PageResult<OperatingPointDomain> result = new PageResult<>(page);

        return result;
    }


    @RequestMapping("/unloadPoint/all")
    @ResponseBody
    Result unloadPointAll() {
        List<OperatingPointDomain> list = operatingPointDao.findByType(OperatingPointType.UNLOAD.getValue());
        return Result.success(list);
    }

    @RequestMapping("/pickupPoint/save")
    @ResponseBody
    Result savePickupPoint(OperatingPointDomain domain) {
        String locationName = domain.getLocationName();
        if (isEmpty(locationName)) {
            return Result.error("至少选择一个点位");
        }
        OperatingPointDomain find = operatingPointDao.findByLocationNameAndType(locationName, OperatingPointType.PICKUP.getValue());
        if (find != null) {
            return Result.error("点位已存在");
        }
        ShelfDomain find2 = shelfDao.findByLocationName(locationName);
        if (find2 != null) {
            return Result.error("点位已存在");
        }

        domain.setType(OperatingPointType.PICKUP.getValue());
        if(domain.getCreatedDate()==null) {
            domain.setCreatedDate(new Date());
        }
        OperatingPointDomain saved = operatingPointDao.save(domain);
        return Result.success(saved.getId());
    }

    @RequestMapping("/pickupPoint/list")
    @ResponseBody
    PageResult<OperatingPointDomain> listPickupPoint(PageParam param) {

        PageRequest pr = PageRequest.of(param.getStart() / param.getLength(), param.getLength());

        Page<OperatingPointDomain> page = operatingPointDao.findByType(OperatingPointType.PICKUP.getValue(), pr);

        PageResult<OperatingPointDomain> result = new PageResult<>(page);

        return result;
    }

    @RequestMapping("/pickupPoint/all")
    @ResponseBody
    Result pickupPointAll() {
        List<OperatingPointDomain> list = operatingPointDao.findByType(OperatingPointType.PICKUP.getValue());
        return Result.success(list);
    }


    @RequestMapping("/operationPoint/delete")
    @ResponseBody
    Result deleteUnloadPoint(Integer id) {
        operatingPointDao.deleteById(id);
        return Result.success();
    }

    @RequestMapping("/operationPoint/deleteAll")
    @ResponseBody
    Result deleteUnloadPointAll() {
        operatingPointDao.deleteAll();
        return Result.success();
    }

    @RequestMapping("/operationPoint/toggleStatus")
    @ResponseBody
    Result toggleStatus(Integer id) {
        OperatingPointDomain sd = operatingPointDao.getOne(id);
        int status = sd.getLoadStatus();
        System.out.println("toggleLock start=======>"+status);
        sd.setLoadStatus(status== LoadStatusEnum.EMPTY.status ? LoadStatusEnum.NON_EMPTY.status : LoadStatusEnum.EMPTY.status);
        System.out.println("toggleLock end=======>"+sd.getLoadStatus());
        operatingPointDao.save(sd);
        return Result.success();
    }
}
